#include "BloomDemo.h"
#include "img_utils.h" 

BloomDemo::BloomDemo() {
    setAcceptDrops(true);

    // 增加脱离主窗口的控件
    QWidget* toolWidget = new QWidget(this);
    toolWidget->setWindowTitle("Parameters");
    toolWidget->setWindowFlags(Qt::Tool | Qt::WindowTitleHint);
    toolWidget->show();
    // 设置布局
    QVBoxLayout* toolLayout = new QVBoxLayout;
    toolWidget->setLayout(toolLayout);
    toolLayout->setSizeConstraint(QLayout::SetFixedSize);
    // enable选择按钮
    m_enableEffect = new QGroupBox(toolWidget);
    m_enableEffect->setCheckable(true);
    m_enableEffect->setTitle("Enable");
    m_enableEffect->setFlat(true);
    // 按钮动作
    connect(m_enableEffect, &QGroupBox::toggled, this, &BloomDemo::enableEffect);
    toolLayout->addWidget(m_enableEffect);
    // bright按钮
    m_brightnessSlider = new QSlider(toolWidget);
    m_brightnessSlider->setRange(0, 50);
    m_brightnessSlider->setOrientation(Qt::Horizontal);
    m_brightnessSlider->setFixedSize(200, 20);
    connect(m_brightnessSlider, &QSlider::sliderMoved, this, QOverload<>::of(&QWidget::update));
    // 设置enable group按钮的布局并增加blur滑动条
    QGridLayout* layout = new QGridLayout;
    m_enableEffect->setLayout(layout);
    layout->addWidget(new QLabel("Brightness"), 1, 0);
    layout->addWidget(m_brightnessSlider, 1, 1);
}

void BloomDemo::enableEffect(bool enabled) {
    qDebug() << "enable effect " << enabled;
    update();
}

void BloomDemo::loadImage(const QImage& image) {
    m_image = image;
    if (m_image.isNull()) {
        setFixedSize(512, 256);
        setWindowTitle(QString("Can not load %1").arg(m_fileName));
    } else {
        QString title = "Bloom Effect ";
        if ((m_image.width() > 640) || (m_image.height() > 480)) {
            qDebug() << "Image is too large. Rescaling....";
            int w = m_image.width();
            m_image = m_image.scaled(640, 480, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            title += QString("[Zoom %1%] ").arg(m_image.width() * 100 / w);
        }
        setWindowTitle(QString("%1: %2 (%3 x %4)").arg(title).arg(m_fileName).
                arg(m_image.width()).arg(m_image.height()));
        setFixedSize(m_image.width(), m_image.height());
    }
    update();
}

void BloomDemo::setFileName(const QString& fileName) {
    // 检查文件是否存在
    QString curFile = QFileInfo(fileName).canonicalFilePath();    
    if (! QFile::exists(curFile)) {
        qDebug() << "File " << fileName << " not found";
        exit(EXIT_FAILURE);
    }
    m_fileName = QFileInfo(fileName).fileName();
    loadImage(QImage(fileName));
}

void BloomDemo::paintEvent(QPaintEvent*) {
    QPainter painter(this);
    if (!m_enableEffect->isChecked())
        painter.drawImage(0, 0, m_image);
    else {
        int brightness = m_brightnessSlider->value();
        painter.drawImage(0, 0, brightened(m_image, brightness));
    }
}

void BloomDemo::dragEnterEvent(QDragEnterEvent* event) {
    if (event->mimeData()->hasFormat("text/uri-list"))
        event->acceptProposedAction();
}

void BloomDemo::dropEvent(QDropEvent* event) {
    QList<QUrl> urls = event->mimeData()->urls();
    if (urls.count()) {
        QString fname = urls[0].toLocalFile();
        m_fileName = QFileInfo(fname).fileName();
        if (event->mimeData()->hasImage()) {
            qDebug() << "Drop a image data...";
            QImage img = qvariant_cast<QImage>(event->mimeData()->imageData());
            loadImage(img);
        } else {
            qDebug() << "Drop a image file " << m_fileName;
            loadImage(QImage(fname));
        }
        event->acceptProposedAction();
    }
}
